package com.ka.magicexplorer.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;

import com.ka.magicexplorer.activity.R;
import com.ka.magicexplorer.model.GroupItem;
import com.ka.magicexplorer.model.GroupParent;

public class AdapterMenuLeft extends BaseExpandableListAdapter {

	protected List<GroupParent> listData;
	protected Context context;

	public AdapterMenuLeft(Context ct) {
		context = ct;
		listData = new ArrayList<GroupParent>();
		generateData();
	}

	private void generateData() {
		GroupParent gpdocument = new GroupParent(1, context.getResources()
				.getString(R.string.document), 0);
		GroupParent gpgallery = new GroupParent(2, context.getResources()
				.getString(R.string.gallery), 0);
		GroupParent gpmedia = new GroupParent(3, context.getResources()
				.getString(R.string.media), 0);

		List<GroupItem> itemDocument = new ArrayList<GroupItem>();
		GroupItem itempdf = new GroupItem(1, "pdf", R.drawable.ic_pdf);
		GroupItem itemdoc = new GroupItem(2, "doc", R.drawable.ic_doc);
		GroupItem itemtxt = new GroupItem(3, "txt", R.drawable.ic_txt);

		itemDocument.add(itemdoc);
		itemDocument.add(itempdf);
		itemDocument.add(itemtxt);

		gpdocument.setGi(itemDocument);

		List<GroupItem> itemMedia = new ArrayList<GroupItem>();
		GroupItem itemMusic = new GroupItem(1, "music", R.drawable.ic_music);
		GroupItem itemVideo = new GroupItem(2, "video", R.drawable.ic_movie);

		itemMedia.add(itemMusic);
		itemMedia.add(itemVideo);

		gpmedia.setGi(itemMedia);

		List<GroupItem> itemGallery = new ArrayList<GroupItem>();
		GroupItem itemjpg = new GroupItem(1, "jpg", R.drawable.ic_jpg);
		GroupItem itempng = new GroupItem(2, "png", R.drawable.ic_png);
		GroupItem itemgif = new GroupItem(2, "gif", R.drawable.ic_gif);

		itemGallery.add(itemjpg);
		itemGallery.add(itempng);
		itemGallery.add(itemgif);

		gpgallery.setGi(itemGallery);

		listData.add(gpdocument);
		listData.add(gpgallery);
		listData.add(gpmedia);
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return listData.get(groupPosition).getGi().get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return (listData.get(groupPosition).getGi()).get(childPosition).getId();
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {

		if (convertView == null) {
			LayoutInflater inflat = LayoutInflater.from(parent.getContext());
			convertView = inflat.inflate(R.layout.item_group_item, null);
		}

		ImageView imv = (ImageView) convertView
				.findViewById(R.id.menuleft_item_image);
		imv.setBackgroundResource(((GroupItem) getChild(groupPosition,
				childPosition)).getImageID());
		
		
//		if( !((ExpandableListView)parent).isGroupExpanded(groupPosition))
//		{
//			Animation animation = new  AlphaAnimation(0.0f, 1.0f);
//	        animation.setDuration(200);
//	        convertView.setAnimation(animation);
//		}

		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return listData.get(groupPosition).getGi().size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return listData.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return listData.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return listData.get(groupPosition).getId();
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {

		if (convertView == null) {
			LayoutInflater inflat = LayoutInflater.from(parent.getContext());
			convertView = inflat.inflate(R.layout.item_group_parent, null);
		}

		TextView tv = (TextView) convertView
				.findViewById(R.id.menuleft_parent_name);
		tv.setText(((GroupParent) getGroup(groupPosition)).getName());
		tv.setTextColor(Color.WHITE);
		
		final ExpandableListView elv = (ExpandableListView)parent;
		final int focus_position = groupPosition;
		final boolean isCollapse = !isExpanded;
		
		ImageView imv = (ImageView)convertView.findViewById(R.id.expandableIcon);
		imv.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if( isCollapse)
					elv.expandGroup(focus_position);
				else
					elv.collapseGroup(focus_position);
					
				elv.invalidateViews();
			}
		});

		convertView.setBackgroundColor(parent.getResources().getColor(R.color.color_menu_group_background));
		
		if (getChildrenCount(groupPosition) != 0) {
			int imageResourceId = isExpanded ? android.R.drawable.arrow_up_float
					: android.R.drawable.arrow_down_float;
			imv.setImageResource(imageResourceId);
			imv.setVisibility(View.VISIBLE);
		} else {
			imv.setVisibility(View.INVISIBLE);
		}
		

        
		
		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

}
